from datetime import datetime, date
from typing import Optional

from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel
from pydantic_validation_decorator import NotBlank

from module_admin.annotation.pydantic_annotation import as_query


class InfoModel(BaseModel):
    """
    股票基础信息表对应pydantic模型
    """
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)

    stock_code: Optional[str] = Field(default=None, description='股票代码')
    stock_name: Optional[str] = Field(default=None, description='股票名称')
    exchange_id: Optional[str] = Field(default=None, description='交易所代码')
    ipo_date: Optional[date] = Field(default=None, description='上市日')
    expire_date: Optional[date] = Field(default=None, description='退市日')
    instrument_status: Optional[str] = Field(default=None, description='证券状态')
    interface_data: Optional[str] = Field(default=None, description='接口获取到的该股票数据报文')
    interface_data_md5: Optional[str] = Field(default=None, description='接口数据报文MD5')
    update_time: Optional[datetime] = Field(default=None, description='更新时间')
    create_time: Optional[datetime] = Field(default=None, description='创建时间')

    @NotBlank(field_name='stock_name', message='股票名称不能为空')
    def get_stock_name(self):
        return self.stock_name

    def validate_fields(self):
        self.get_stock_name()


class InfoQueryModel(InfoModel):
    """
    股票基础信息不分页查询模型
    """
    pass


@as_query
class InfoPageQueryModel(InfoQueryModel):
    """
    股票基础信息分页查询模型
    """

    page_num: int = Field(default=1, description='当前页码')
    page_size: int = Field(default=10, description='每页记录数')
